package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.info.GYScrollViewInfo;
import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

public class GYScrollView extends NestedScrollView implements GYScrollViewInfo {
    public GYScrollView(@NonNull Context context) {
        this(context, null);
    }

    public GYScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GYScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private int maxWidthPx = WRAP_CONTENT;
    private int maxHeightPx = WRAP_CONTENT;
    private int overScrollMode = OVER_SCROLL_NEVER;

    private void init(AttributeSet attributeSet) {
        if (attributeSet != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attributeSet, R.styleable.GYScrollView);
            maxWidthPx = typedArray.getDimensionPixelSize(R.styleable.GYScrollView_maxWidth, maxWidthPx);
            maxHeightPx = typedArray.getDimensionPixelSize(R.styleable.GYScrollView_maxHeight, maxHeightPx);
            overScrollMode = typedArray.getInteger(R.styleable.GYScrollView_android_overScrollMode, overScrollMode);
            typedArray.recycle();
        }
        setOverScrollMode(overScrollMode);
    }

    public int getMaxWidthPx() {
        return maxWidthPx;
    }

    public GYScrollView setMaxWidthDp(int maxWidthDp) {
        return setMaxSizePx(GYDisplayUtils.dp2px(getContext(), maxWidthDp), maxHeightPx);
    }

    public GYScrollView setMaxWidthPx(int maxWidthPx) {
        return setMaxSizePx(maxWidthPx, maxHeightPx);
    }

    public int getMaxHeightPx() {
        return maxHeightPx;
    }

    public GYScrollView setMaxHeightDp(int maxHeightDp) {
        return setMaxSizePx(maxWidthPx, GYDisplayUtils.dp2px(getContext(), maxHeightDp));
    }

    public GYScrollView setMaxHeightPx(int maxHeightPx) {
        return setMaxSizePx(maxWidthPx, maxHeightPx);
    }

    public GYScrollView setMaxSizePx(int maxWidthPx, int maxHeightPx) {
        this.maxWidthPx = maxWidthPx;
        this.maxHeightPx = maxHeightPx;
        requestLayout();
        invalidate();
        return this;
    }


    private int widthMeasureSpec;
    private int heightMeasureSpec;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (maxWidthPx != WRAP_CONTENT) {
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidthPx, MeasureSpec.AT_MOST);
        }
        if (maxHeightPx != WRAP_CONTENT) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeightPx, MeasureSpec.AT_MOST);
        }
        this.widthMeasureSpec = widthMeasureSpec;
        this.heightMeasureSpec = heightMeasureSpec;
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
